home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / contrib / zelk / src-zlib / pathops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-17  |  2.6 KB  |  151 lines

  1. /* pathops.c - pathname operations package from the net
  2.  * todo:
  3.  * modified(zilla)
  4.  * 26sep
  5.  * 18feb        logic change in getpath (Beware!)
  6.  * sep/1        more lisp compatible
  7.  * 1. make separator char independent of os
  8.  * 2. return pointer to statically allocated string
  9.  *
  10.  * pathnam operations
  11.  * returns an allocated string representing:
  12.  * 
  13.  * ext    - the file extension                    (getext)
  14.  * root    - everything but the extension          (delext) 
  15.  * tail    - the file and extension less the path  (getname) delpath
  16.  * head    - the path less the file                (getpath) delname
  17.  *
  18.  */
  19.  
  20. # include <theusual.h>
  21. # include <constants.h>
  22.  
  23. /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  24.  
  25. #if Emac
  26.  static char sepchar = ':';
  27.  static char *sepchars = ":";
  28. #endif
  29. #if Eunix
  30.  static char sepchar = '/';
  31.  static char *sepchars = "/";
  32. #endif
  33.  
  34. static char *myalloc Zproto((char *));
  35.  
  36. static char *myalloc(str)
  37.   char *str;
  38. {
  39.   static char path[CMAXPATH];
  40.   str_cpy(path,str);
  41.   return(path);
  42. }
  43.  
  44. /*forward*/ char *Zpathop Zproto((char *,Dchar));
  45.  
  46. char *
  47. Zpathgetpath(pathname)
  48.   char *pathname;
  49. {
  50.   return Zpathop(pathname,'h');
  51. }
  52.  
  53. char *
  54. Zpathgetext(pathname)
  55.   char *pathname;
  56. {
  57.   return Zpathop(pathname,'e');
  58. }
  59.  
  60.  
  61. char *
  62. Zpathgetname(pathname)
  63.   char *pathname;
  64. {
  65.   return Zpathop(pathname,'t');
  66. }
  67.  
  68. char *
  69. Zpathdelext(pathname)
  70.   char *pathname;
  71. {
  72.   return Zpathop(pathname,'r');
  73. }
  74.  
  75. /* now returns a pointer to a static string! */
  76. char *
  77. Zpathop(cp, type)
  78.   char *cp;
  79.   register Dchar type;
  80. {
  81.     register char *wp, *xp;
  82.  
  83.     switch (type) {
  84.  
  85.     case 'h':
  86.     case 't':
  87.     if (!str_any(cp,sepchars)) {
  88.           if (type == 'h')              /*&z added this feb*/
  89.             return myalloc(""); /*((char *)0);*/
  90.           else
  91.             return (myalloc(cp));
  92.         }
  93.     wp = str_end(cp);
  94.     while (*--wp != sepchar)
  95.         continue;
  96.     if (type == 'h') {
  97.         xp = myalloc(cp);
  98.         xp[wp - cp] = 0;
  99.     }
  100.     else
  101.         xp = myalloc(wp + 1);
  102.     return (xp);
  103.  
  104.     case 'e':
  105.     case 'r':
  106.     wp = str_end(cp);
  107.     for (wp--; wp >= cp && *wp != sepchar; wp--)
  108.         if (*wp == '.') {
  109.         if (type == 'e')
  110.             xp = myalloc(wp + 1);
  111.         else {
  112.             xp = myalloc(cp);
  113.             xp[wp - cp] = 0;
  114.         }
  115.         return (xp);
  116.         }
  117.     return (myalloc(type == 'e' ? "" : cp));
  118.     }
  119.     return ((char *)0);    /*default*/
  120. }
  121.  
  122.  
  123. #ifdef TESTIT
  124. /*% cc % %*/
  125. #include <stdio.h>
  126.  
  127. main() {
  128.     char str[80];
  129.     char c,*f;
  130.     while(1) {
  131.     printf("> ");
  132.     scanf("%s %c",str,&c);
  133.     printf(":%s: :%c:\n",str,c);
  134.     f = file(str,c);
  135.     printf("==>%s\n\n",f);
  136.     }
  137. }
  138.  
  139.  
  140. getline(buf,fp)
  141.   register char *buf;
  142.   register FILE *fp;
  143. {
  144.     register char c;
  145.     while((c = getc(fp)) != '\n' && (c != EOF))
  146.     *buf++ = c;
  147.     *buf = NULL;
  148. }
  149.  
  150. #endif /*TESTIT*/
  151.